<template>
  <a-modal :title="form.categoryId ? '编辑' : '添加'" width="600px" :open="visibleFlag" @cancel="onClose" :maskClosable="false" :destroyOnClose="true">
    <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 5 }">
      <a-row>
        <a-col :span="24">
          <a-form-item label="设备类名称" name="name">
            <a-input style="width: 100%" v-model:value="form.name" placeholder="请输入设备类名称" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="传输组" name="groupTrans">
            <a-select v-model:value="form.groupTrans" placeholder="请选择传输组" @change="onGroupChange">
              <a-select-option v-for="option in transmissionGroups" :key="option.value" :value="option.value">{{ option.label }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="多播地址" name="multiAdress">
            <a-input v-model:value="form.multiAdress" :placeholder="multiAdressPlaceholder" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="目的端口" name="destPort">
            <a-input v-model:value="form.destPort" :placeholder="destPortPlaceholder" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="SFI标识" name="sfi">
            <a-input v-model:value="form.sfi" placeholder="请输入SFI标识" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="描述" name="description">
            <a-textarea :rows="4" v-model:value="form.description" placeholder="请输入描述" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-modal>
</template>

<script setup>
  import { reactive, ref, nextTick, onMounted } from 'vue';
  import { defineProps } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';
  import { iotApi } from '/@/api/business/iotSystem/index';
  import { smartSentry } from '/@/lib/smart-sentry';

  // ------------------------ 事件 ------------------------
  const emits = defineEmits(['reloadList']);
  const funList = ref([]);
  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    categoryId: undefined,
    name: undefined,
    groupTrans: undefined,
    multiAdress: undefined,
    destPort: undefined,
    sfi: undefined,
    description: undefined,
  };

  let form = reactive({ ...formDefault });

  // 传输组选项
  const transmissionGroups = [
    { label: "MISC", value: "MISC" },
    { label: "TGTD", value: "TGTD" },
    { label: "SATD", value: "SATD" },
    { label: "NAVD", value: "NAVD" },
    { label: "RCOM", value: "RCOM" },
    { label: "TIME", value: "TIME" },
    { label: "PROP", value: "PROP" },
  ];

  // 多播地址字典
  const multicastAddresses = [
    { link: "MISC", value: "239.192.0.1" },
    { link: "TGTD", value: "239.192.0.2" },
    { link: "SATD", value: "239.192.0.3" },
    { link: "NAVD", value: "239.192.0.4" },
    { link: "VDRD", value: "239.192.0.5" },
    { link: "RCOM", value: "239.192.0.6" },
    { link: "TIME", value: "239.192.0.7" },
    { link: "PROP", value: "239.192.0.8" },
  ];

  // 目的端口字典
  const destPorts = [
    { link: "MISC", value: "60001" },
    { link: "TGTD", value: "60002" },
    { link: "SATD", value: "60003" },
    { link: "NAVD", value: "60004" },
    { link: "VDRD", value: "60005" },
    { link: "RCOM", value: "60006" },
    { link: "TIME", value: "60007" },
    { link: "PROP", value: "60008" },
  ];

  // 联动更新多播地址和目的端口
  const onGroupChange = (value) => {
    const multicast = multicastAddresses.find(item => item.link === value);
    const port = destPorts.find(item => item.link === value);

    if (multicast) form.multiAdress = multicast.value;
    if (port) form.destPort = port.value;
  };

  const rules = {
    name: [{ required: true, message: '请填写名称' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save(form);
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save(formData) {
    SmartLoading.show();
    try {
      if (formData.categoryId) {
        await iotApi.update(formData);
      } else {
        await iotApi.add(formData);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>

<style scoped>
  .ant-select {
    width: 100% !important;
  }
</style>
